package Biz;

import java.util.LinkedList;
import javax.ejb.LocalBean;
import javax.ejb.Stateful;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import Entity.PcrTube;
import Entity.User;

/**
 * Session Bean implementation class userBean
 */
@Stateful
@LocalBean
public class userBean implements userBeanLocal {

    /**
     * Default constructor. 
     */

    
    @PersistenceContext(unitName = "PCR_System")
    private EntityManager manager;

	User currUser=null;
    public userBean() {
        // TODO Auto-generated constructor stub
    }
    public boolean login(String id,String password)
    {
  	     User u=manager.find(User.class, id);
  	     if(u==null)return false;
  	     if(u.getPassword().equals(password))
  	     {
  	    	 currUser=u;
  	    	 return true;
  	     }
  	     else
  	     {
  	    	 return false;
  	     }
    }
    public boolean register(String id,String password,String name)
    {
		User u=new User();
		u.setId(id);
		u.setName(name);
		u.setPassword(password);
		u.setType("TESTED");
		u.setPcrTubes(new LinkedList<PcrTube>());
    	if(manager.find(User.class, u.getId()) == null)
    	{
    		manager.persist(u);
    		currUser=u;
        	return true;
    	}
    	else
    	{
        	return false;
    	}
    }

    public User getUser()
    {
    	return this.currUser;
    }
    public boolean isAdmin()
    {
    	return this.getUser()!=null&&this.getUser().getType().equals("Admin");
    }
    public boolean isEntry()
    {
    	return this.getUser()!=null&&this.getUser().getType().equals("Entry");
    }
    public boolean isCollector()
    {
    	return this.getUser()!=null&&this.getUser().getType().equals("Collector");
    }
    public boolean isTested()
    {
    	return this.getUser()!=null&&this.getUser().getType().equals("Tested");
    }
    
}
